﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class CustomerManagement_AddPresentCoupon : System.Web.UI.Page
{
    private DataTable PresentCouponList
    {
        get
        {
            DataTable presentCouponList = null;
            if (ViewState["PresentCouponList"] == null)
            {
                ViewState["PresentCouponList"] = new BLL.PresentCouponDetail().CreateTempDataTable();
            }
            presentCouponList = (DataTable)ViewState["PresentCouponList"];
            return presentCouponList;
        }
        set
        {
            ViewState["PresentCouponList"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("300400111");
        if (!IsPostBack)
        {
            dlistPresentCoupon.DataSource = this.PresentCouponList;
            dlistPresentCoupon.DataBind();
            btnCancel.Attributes.Add("onclick", "if(!confirm('要取消吗?'))return false;");
        }
        Session["FCode"] = "300400111";
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("300400111", "00001"))
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
        try
        {
            if (this.PresentCouponList.Rows.Count == 0)
            {
                throw new Exception("请添加要赠送给客户的礼券！");

            }
            string newId = "";

            if (new BLL.PresentCouponMain().InsertNew(this.PresentCouponList, Share.UserValue(0), out newId))
            {
                Share.InsertLog("300400111", "00001");
                Response.Write("<script>alert('新增成功！');window.location.href='PresentCouponDetail.aspx?code=" + newId + "'</script>");
            }
            else
            {
                Share.Alert(this.Page, "新增未成功，请重新添加！");
            }

        }
        catch (Exception ex)
        {
            Share.Alert(this.Page, ex.Message);
        }
        StringBuilder sbSql = new StringBuilder();
        

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddPresentCoupon.aspx");
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (hidCustomer.Value == "")
        {
            Share.Alert(this.Page, "请选择会员！");
            return;
        }
        if (hidCoupon.Value == "")
        {
            Share.Alert(this.Page,"请选择礼券！");
            return;
        }
        char[] ch = new char[] {'$'};
        string[] arrCouponSelectedResult = hidCoupon.Value.TrimEnd(ch).Split('$');
        string[] arrCustomerSelectedResult = hidCustomer.Value.Split('|');
        string[] arrTemp;
        for (int i = 0; i < arrCouponSelectedResult.Length; i++)
        {
            arrTemp = arrCouponSelectedResult[i].Split('|');
            if (ValidCouponCodeIsSelected(arrTemp[0]))
            {
                continue;
            }
            new BLL.PresentCouponDetail().AddDataRowToDataTable(this.PresentCouponList, arrCustomerSelectedResult[0], arrCustomerSelectedResult[1], arrTemp[0], Convert.ToDecimal(arrTemp[1]));
        }

        dlistPresentCoupon.DataSource = this.PresentCouponList;
        dlistPresentCoupon.DataBind();

        hidCoupon.Value = "";
        hidCustomer.Value = "";
    }

    /// <summary>
    /// 检查礼券编号是否已经被分配给客户
    /// </summary>
    /// <param name="couponCode"></param>
    /// <returns></returns>
    private bool ValidCouponCodeIsSelected(string couponCode)
    {
        bool result = false;
        for (int j = 0; j < this.PresentCouponList.Rows.Count; j++)
        {
            if (this.PresentCouponList.Rows[j]["couponcode"].ToString() == couponCode)
            {
                result = true;
                break;
            }
        }
        return result;
    }

    protected void dlistPresentCoupon_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "del")
        {
            this.PresentCouponList.Rows.RemoveAt(e.Item.ItemIndex);
            dlistPresentCoupon.DataSource = this.PresentCouponList;
            dlistPresentCoupon.DataBind();
        }
    }
}
